package com.example.mapper;

import com.example.domain.Resource;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResourceMapper {
    @Select("select * from [dbo].[Resource] where RNUM = #{id} AND SHOW = 1")
    Resource selectResourceById(String id);

    @Select("select * from [dbo].[Resource] where UID = #{id} AND SHOW = 1")
    List<Resource> selectResourceByUid(String id);

    @Select("select * from [dbo].[Resource] where SHOW = 1")
    List<Resource> showAllResource();

    @Select("select * from [dbo].[Resource] where SHOW = 0")
    List<Resource> showDeleteResource();

    @Select("select * from [dbo].[Resource] where concat (ISNULL(RNUM,''),ISNULL(RNO,''),ISNULL(RNAME,''),ISNULL(RTYPE,''),ISNULL(STORAGE,'')" +
            ",ISNULL(BRAND,''),ISNULL(MODEL,''),ISNULL(PRICE,''),ISNULL(UID,'')) LIKE '%${object}%' AND SHOW=1")
    List<Resource> searchResourceByAll(String object)throws Exception;

    @Select("select count(*) from [dbo].[Resource] where RNUM = #{rid}")
    Integer hasId(String rid);

    @Update("update [dbo].[Resource] set UID=#{uid},RSTATUS=#{rstatus}," +
            "BORROWTIME=#{borrowtime},STORAGE=#{storage},TIPS=#{tips} where RNUM = #{rnum}")
    Boolean borrowUpdate(Resource resource)throws Exception;

    @Update("update [dbo].[Resource] set UID=null,RSTATUS=#{rstatus}," +
            "BORROWTIME=null,STORAGE=#{storage},TIPS=#{tips} where RNUM = #{rnum}")
    Boolean returnUpdate(Resource resource)throws Exception;

    @Insert("insert into [dbo].[Resource] (RNUM,RNAME,RSTATUS,RTYPE,BRAND,MODEL,PRICE,REGISTIME,STORAGE,MANAGE_UID,TIPS)"+
            "values(#{rnum},#{rname},#{rstatus},#{rtype},#{brand},#{model},#{price},#{registime},#{storage},#{manage_uid},#{tips})")
    Boolean insert(Resource resource)throws Exception;

    //假删除
    @Update("update [dbo].[Resource] set SHOW = 0 where RNUM=#{id}")
    Boolean delete(int id)throws Exception;

    //当盘点不一致的时候修改人员为当前保管人员
    @Update("update [dbo].[Resource] set UID = #{uid} where RNUM = #{rid}")
    Boolean updateUid(String rid,Integer uid);

    //真删除
    @Delete("delete from [dbo].[Resource] where RNUM = #{id}")
    Boolean trueDelete(int id) throws Exception;

    //通过resourceid去查找userid
    @Select("select UID from [dbo].[Resource] where RNUM = #{id}")
    Integer selectUidById(String id);

    //通过uid去查找resourceid
    @Select("select RNUM from [dbo].[Resource] where UID = #{id}")
    List<String> selectRidByUid(Integer id);
}
